// packageA/member/balance/transformLoveV/transformLoveV.js
var app = getApp();
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
let isclick = true;
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    //爱心转账值
    sell_value: "",
    //爱心会员ID
    sell_id: "",
    // 转让手续费比率
    poundage: 0,
    trading_limit: 0,
    trading_fold: 0,
    love_name: "", //爱心值自定义名称
    usable: 0, // 登陆会员可用爱心值
    rate: 0,
    isTransformLoveV: false, //判断是否为转化爱心值
    balanceLang: "余额" //金额自定义
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.name == 'transformLoveV') {
      this.setData({
        isTransformLoveV: true
      })
    }
    this.getUsable();
    this.getLove();
    try {
      var value = wx.getStorageSync('balance');
      if (value) {
        this.setData({
          balanceLang: value
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    isclick = true;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  allConversion() {
    this.setData({
      sell_value: this.data.usable
    })
  },
  getUsable() {
    let that = this;
    if (this.data.isTransformLoveV) {
      let urlStr = app.getNetAddresss('plugin.love.Frontend.Controllers.page.index');
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              love_name: res.data.love_name
            });
            console.log(that.data.themeBaseColor, 'ddddddddd')
            wx.setNavigationBarColor({
              frontColor: '#ffffff',
              backgroundColor: that.data.themeBaseColor,
              animation: {
                duration: 400,
                timingFunc: 'easeIn'
              }
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    } else {
      let urlStr = app.getNetAddresss('finance.balance.conver');
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              usable: res.data.credit2,
              rate: res.data.rate,
              rate_1: Math.floor(((100 * res.data.rate) / 100) * 100) / 100,
              rate_2: Math.floor(((that.data.sell_value * res.data.rate) / 100) * 100) / 100
            });
            wx.setNavigationBarColor({
              frontColor: '#ffffff',
              backgroundColor: that.data.themeBaseColor,
              animation: {
                duration: 400,
                timingFunc: 'easeIn'
              }
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  getLove() {
    try {
      var value = wx.getStorageSync('yz_basic_info');
      if (value) {
        let val = value.home;
        if (val.designer.love_name) {
          this.setData({
            love_name: val.designer.love_name
          });
        }
        wx.setNavigationBarTitle({
          title: '转化' + val.designer.love_name
        });

      }
    } catch (e) {
      // Do something when catch error
    }
    if (this.data.isTransformLoveV) {
      let that = this;
      let urlStr = app.getNetAddresss('plugin.love.Frontend.Modules.Trading.Controllers.trading.get-sell-love');
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              usable: res.data.love.usable,
              poundage: res.data.set.poundage,
              trading_limit: res.data.set.trading_limit,
              trading_fold: res.data.set.trading_fold
            });
            if (res.data.set.trading_fetter) {
              that.setDtaa({
                trading_fetter: res.data.set.trading_fetter
              });
            }
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  sellValueinp(e) {
    let val = e.detail.value;
    this.setData({
      sell_value: val,
      rate_2: Math.floor(((val * this.data.rate) / 100) * 100) / 100
    });
  },
  saleBalance() {
    let that = this;
    if (this.data.isTransformLoveV && !this.data.sell_value) {
      wx.showToast({
        icon: 'none',
        title: '请输入您要出售的数额',
        duration: 1500
      });
      return;
    }
    if (isclick) {
      isclick = false;
      if (this.data.isTransformLoveV) {
        let urlStr = app.getNetAddresss('plugin.love.Frontend.Modules.Trading.Controllers.trading.save-sell-love');
        app._getNetWork({
          url: urlStr,
          data: {
            amount: this.data.sell_value
          },
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              setTimeout(function () {
                isclick = true;
              }, 1000);
              // if (res.result === 1) {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
                that.setData({
                  sell_value: ''
                });
                that.getLove();
              // } 
            }else {
              isclick = true;
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          },
          fail: function (res) {
            setTimeout(function () {
              isclick = true;
            }, 1000);
            console.log(res);
          }
        });
      } else {
        let urlStr = app.getNetAddresss('finance.balance.convert-love-value');
        app._getNetWork({
          url: urlStr,
          data: {
            convert_amount: this.data.sell_value
          },
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              setTimeout(function () {
                isclick = true;
              }, 1000);
              if (res.result === 1) {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
                that.setData({
                  sell_value: ''
                });
                that.getUsable();
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
            }
          },
          fail: function (res) {
            setTimeout(function () {
              isclick = true;
            }, 1000);
            console.log(res);
          }
        });
      }
    }

  }
});